﻿using System;
using System.Web;
using System.Web.UI;

public partial class userControls_ctrl_large_viewgallery : System.Web.UI.UserControl
{
    private Bintje.Trail m_trail;

    public Bintje.Trail Trail
    {
        get { return m_trail; }
        set { m_trail = value; }
    }

    public int? TrailID
    {
        get
        {
            if (hfID.Value == "0")
            {
                return null;
            }
            
            return Convert.ToInt32(hfID.Value);
        }
        set
        {
            hfID.Value = value.ToString();
        }
    }

    public int PhotoCount
    {
        get
        {
            return int.Parse(hfPhotoCount.Value);
        }
        set
        {
            hfPhotoCount.Value = value.ToString();
        }
    }
   
    public bool ViewTrail
    {
        set
        {
            if (value)
            {
                trGallery.Style[HtmlTextWriterStyle.Display] = "none";
                trTrail.Style[HtmlTextWriterStyle.Display] = "";
            }
            else
            {
                trGallery.Style[HtmlTextWriterStyle.Display] = "";
                trTrail.Style[HtmlTextWriterStyle.Display] = "none";
            }
        }
        get
        {
            return true;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowTrailImages();
        }
    }

    public void ShowTrailImages()
    {
        if (this.TrailID.HasValue)
        {
            int tid = this.TrailID.Value;
            
            if (tid > 0)
            {
                LoadTrailPics(tid);
            }
        }
    }
    private void LoadTrailPics(int tid)
    {
        bool check = true; // to chck if there is no image then disable
        //Bintje.Trail trail = Bintje.Trail.Get(tid);
        if (null != m_trail && null != m_trail.Waypoints && m_trail.Waypoints.Count > 0)
        {

            for (int i = 0; i < m_trail.Waypoints.Count; i++)
            {
                if (null != m_trail.Waypoints[i].ImageUrl && m_trail.Waypoints[i].ImageUrl.Length > 0)
                {
                    if (string.IsNullOrEmpty(imgMainTrail.ImageUrl))
                    {
                        imgMainTrail.ImageUrl = m_trail.Waypoints[i].ImageUrl[0];
                        /*imgMainImage.ImageUrl = */imgMain.ImageUrl = m_trail.Waypoints[i].ImageUrl[0];
                    }
                    for (int j = 0; j < m_trail.Waypoints[i].ImageUrl.Length; j++)
                    {
                        string imgID = "";
                        string imgClientID = imgMain.ClientID;
                        ltrImages.Text += "<li ><img " + imgID + " src='" + m_trail.Waypoints[i].ImageUrl[j] + "' alt='' onclick='return changeTrailPic(\"" + imgClientID + "\",this);' width='75' height='75' style='cursor:pointer;' /></li>\n";
                        check = false;
                    }
                }
            }
        }
        if (check)
        {
            //imgMainImage.Visible = false;
            imgMainTrail.Visible = false;
            imgMain.Visible = false;
            divcheck.Visible = false;
            ltrImages.Text = "";
        }
    }

    protected void lbSearrchSimilar_Click(object sender, EventArgs e)
    {
        string pictUrl = imgMainTrail.ImageUrl;
        if (!string.IsNullOrEmpty(pictUrl))
        {
            System.Text.RegularExpressions.Regex re =
                new System.Text.RegularExpressions.Regex(@"id=\d+");
            if (re.IsMatch(pictUrl))
            {
                string imgId = re.Match(pictUrl).Value.Replace("id=", string.Empty);
                Response.Redirect(string.Format("~/UI/view-FoundImages.aspx?TrailID={0}&ImageID={1}", this.TrailID, imgId));
            }
        }
    }
}
